/*********************************************************************************************************
* Movement in form of a cursor. Like in a 2d Car racing
*
*  Author of the ported code: Florian Weil - www.derhess.de
*
************************************************************************************************************/
intlets = 1;
outlets = 2;

// distance between two points or coordinate
var angle = 45;
var rotationSpeed = 5;
var velocity = 4;


var canvas_width = 640;
var canvas_height = 480;

var x_pos = canvas_width / 2;
var y_pos = canvas_height / 2;
var vx = 0;
var vy = 0;

function setRotationSpeed(value)
{
	if(value > -361 && value < 361) {
		rotationSpeed = value;
		angle += rotationSpeed; 
	} else {
		post("ERROR: Angle value is wrong!! Value must be between 0 and 360 degree!\n");
	}
}

function setVelocity(value)
{
	if(value > 0)
		velocity = value;
	else
		post("ERROR: velocity value is negative. It must be greater than 0!\n");
}

function setAngle(value)
{
	if(value > -1 && value < 361)
		angle = value;
	else
		post("ERROR: Angle value is wrong!! Value must be between 0 and 360 degree!\n");
}



function move() 
{
	//var radians = angle * Math.PI / 2;
	
	vx = Math.cos(angle) * velocity;
	vy = Math.sin(angle) * velocity;
	
	x_pos += vx;
	y_pos += vy;

}

function reset()
{
	velocity = 5;
	angle = 0;
	x_pos = canvas_width / 2;
	y_pos = canvas_height / 2;
}

//////////////////////////////////////////////////////////////////////////
//    MAX MSP FUNCTION
//////////////////////////////////////////////////////////////////////////
function bang()
{
	move();
	output();
}

//////////////////////////////////////////////////////////////////////////
//    Helper Functions
//////////////////////////////////////////////////////////////////////////
function output()
{
	outlet(0, x_pos);
    outlet(1, y_pos);
	//post("rectangleMovement: Send Bang: " + x_pos + "/" + y_pos + "\n");
}
